package meijuli.com.join51.business.message;

import android.app.Activity;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.Announcement;
import meijuli.com.join51.business.model.Message;

import java.util.ArrayList;
import java.util.List;

import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 *
 *
 * @author jack 2017-05-15 10:08
 */
public class MessagePresenterImpl implements Contract.MessagePresenter {
    private Contract.MessageView mMessageView;
    private int mPageSize = 10;
    private Activity mActivity;

    public MessagePresenterImpl(Contract.MessageView messageView,Activity activity) {
        mMessageView = messageView;
        mActivity = activity;
    }

    @Override
    public void loadMessage(int pageindex, String userId, boolean isShowLoading) {
            if(isShowLoading) {
                mMessageView.beforeLoad();
            }
            Call<HttpResponse<List<Message>>> call = ApiManager.get().
                    getApiService(ApiService.class).
                    getMessageList(userId ,pageindex, mPageSize);
            HttpRequest.getRequest().autoRecycle(mActivity).start(call, new ResponseCallback<List<Message>>() {

                @Override
                public void onSuccess(String url, List<Message> data) {
                    mMessageView.afterLoad();
                    mMessageView.loadMessage(data);
                }

                @Override
                public void onError(String url, int errorCode, String errorMsg) {
                    super.onError(url, errorCode, errorMsg);
                    mMessageView.loadError();
                }
            });
    }

    @Override
    public void loadAnnouncement(int pageindex, String userId, boolean isShowLoading) {
        if(isShowLoading) {
            mMessageView.beforeLoad();
        }
        Call<HttpResponse<List<Announcement>>> call = ApiManager.get().
                getApiService(ApiService.class).
                getAnnouncement(userId, pageindex, mPageSize);
        HttpRequest.getRequest().autoRecycle(mActivity).start(call, new ResponseCallback<ArrayList<Announcement>>() {

            @Override
            public void onSuccess(String url, ArrayList<Announcement> data) {
                mMessageView.afterLoad();
                mMessageView.loadAnnouncement(data);
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                mMessageView.loadError();
            }
        });
    }
}
